# Required helper functions:
source("00_Scripts/00_helper_function.R")

# Load data:
load("01_Data/spatial_dimension_models.Rdata")
load("01_Data/spatial_dimension_data.Rdata")
load("01_Data/spatial_dimension_weight_matrix.Rdata")

#### Duplicate Analysis ####

# update models with duplicate variable
model_base$duplicate10 <-  get_duplciates(df = model_base, distance_threshold = 10000)
model_base$duplicate20 <-  get_duplciates(df = model_base, distance_threshold = 20000)
model_base$duplicate30 <-  get_duplciates(df = model_base, distance_threshold = 30000)
model_base$duplicate40 <-  get_duplciates(df = model_base, distance_threshold = 40000)

models <- list(lm1, lm2, lm3,
               sm1, sm2, sm3,
               lmer1 , lmer2, lmer3)


models_duplicate10 <- lapply(models,update, .~.+duplicate10)
models_duplicate20 <- lapply(models,update, .~.+duplicate20)
models_duplicate30 <- lapply(models,update, .~.+duplicate30)
models_duplicate40 <- lapply(models,update, .~.+duplicate40)


#### Tables A6 ####
models_df_duplicate10 <- bind_rows(lapply(models_duplicate10,get_model_df))
models_df_duplicate20 <- bind_rows(lapply(models_duplicate20,get_model_df))
models_df_duplicate30 <- bind_rows(lapply(models_duplicate30,get_model_df))
models_df_duplicate40 <- bind_rows(lapply(models_duplicate40,get_model_df))


get_table(models_df_duplicate10, file_name="03_Tables/appendix_A6_1.docx", full_table = F)
get_table(models_df_duplicate20, file_name="03_Tables/appendix_A6_2.docx",  full_table = F)
get_table(models_df_duplicate30, file_name="03_Tables/appendix_A6_3.docx",  full_table = F)
get_table(models_df_duplicate40, file_name="03_Tables/appendix_A6_4.docx",  full_table = F)

rm(list = ls())
